let casConfig = null;
class Config {
  constructor(config) {
    this.data = config;
  }
  get(key,defaultValue=''){
    if(key.indexOf('.') === -1){
      if(this.data[key]){
        return this.data[key];
      }
      return defaultValue;
    }else{
      let keyArr = key.split('.');
      return keyArr.reduce((obj,cur,i) => {
        return obj[cur] ? obj[cur] : defaultValue;
      },this.data);
    }
  }
  set(key,value){
    if(key.indexOf('.') === -1){
      this.data[key] = value;
    }else{
      let keyArr = key.split('.'),tmp={},oldData = this.data;
      keyArr.reduce((obj,cur,i)=>{
        return obj[cur] = i === keyArr.length - 1 ? value :(obj[cur] || {});
      },tmp);
      this.data = {
        ...oldData,
        ...tmp
      }
    }
    this.data[key] = value;
  }
  remove(key){
    delete this.data[key];
  }
  removeAll(){
    this.data = undefined;
  }
  all(){
    return this.data;
  }
  getPipe(arr,data,defaultValue){
    let tmpData = data[arr.splice(0,1)[0]];
    if(arr.length == 0){
      return tmpData;
    }else{
      if(tmpData){
        return this.getPipe(arr,tmpData,defaultValue);
      }else{
        return defaultValue;
      }
    }
  }
}

export default function(config) {
  casConfig = new Config(config);
  return casConfig;
} 
export {
  casConfig
}